package com.ieasy.module.site.front.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.SystemContext;
import com.ieasy.module.common.web.action.BaseController;
import com.ieasy.module.site.service.IColumnService;
import com.ieasy.module.site.service.IDocService;
import com.ieasy.module.site.service.IMagicPicService;
import com.ieasy.module.site.web.form.ColumnForm;
import com.ieasy.module.site.web.form.DocForm;
import com.ieasy.module.site.web.form.MagicPicForm;

@Controller
@RequestMapping("/front/site")
public class SiteIndexAction extends BaseController {

	@Inject
	private IColumnService columnService ;
	
	@Inject
	private IMagicPicService magicService ;

	@Inject
	private IDocService docService ;
	
	@RequestMapping("/nav.do")
	@ResponseBody
	public List<ColumnForm> nav(ColumnForm form) {
		return this.columnService.indexNav(form) ;
	}
	
	@RequestMapping("/magic.do")
	@ResponseBody
	public List<MagicPicForm> magic(String identity) {
		return this.magicService.getListAllByIdentiry(identity) ;
	}
	
	@RequestMapping("/productTypeList.do")
	@ResponseBody
	public List<ColumnForm> productTypeList(String pid) {
		return this.columnService.tree(pid) ;
	}
	
	@RequestMapping("/productList.do")
	@ResponseBody
	public DataGrid productList(DocForm form) {
		if(null != form.getPageSize()) {
			
			SystemContext.setPage(form.getCurPage()) ;
			SystemContext.setRows(form.getPageSize()) ;
		}
		return this.docService.datagrid(form) ;
	}
	
	
	@RequestMapping("/newsTypeList.do")
	@ResponseBody
	public List<ColumnForm> newsTypeList(String pid) {
		return this.columnService.tree(pid) ;
	}
	
	@RequestMapping("/newsList.do")
	@ResponseBody
	public DataGrid newsList(DocForm form) {
		if(null != form.getPageSize()) {
			
			SystemContext.setPage(form.getCurPage()) ;
			SystemContext.setRows(form.getPageSize()) ;
		}
		return this.docService.datagrid(form) ;
	}
	
	
	@RequestMapping("/aboutTypeList.do")
	@ResponseBody
	public List<ColumnForm> aboutTypeList(String pid) {
		return this.columnService.tree(pid) ;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/aboutContent.do")
	@ResponseBody
	public DocForm aboutContent(DocForm form) {
		DocForm df = new DocForm() ;
		if(null != form.getPageSize()) {
			
			SystemContext.setPage(form.getCurPage()) ;
			SystemContext.setRows(form.getPageSize()) ;
		}
		DataGrid datagrid = this.docService.datagrid(form);
		
		List<DocForm> rows = (List<DocForm>) datagrid.getRows() ;
		if(!rows.isEmpty()) {
			df = rows.get(0) ;
			DocForm docForm = this.docService.get(df) ;
			df = docForm ;
		}
		
		return df ;
	}
	
	
	
	
	@RequestMapping("/showDocPage.do")
	public String showDocPage(DocForm form, Model model) {
		model.addAttribute("type", (null != form && form.getType()!=null?form.getType():"other")) ;
		model.addAttribute("doc", this.docService.get(form)) ;
		return "page_view";
	}
	
	@RequestMapping("/showDoc.do")
	public DocForm showDoc(DocForm form, Model model) {
		return this.docService.get(form) ;
	}
}
